import UIKit

class DatePickerExampleViewController: UIViewController {
    
    // MARK: - 控件属性
    private let datePicker = UIDatePicker()
    private let dateLabel = UILabel()
    private let modeSegmentedControl = UISegmentedControl(items: ["日期", "时间", "日期和时间", "倒计时"])
    private let formatButton = UIButton(type: .system)
    
    // 日期格式化器
    private let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        return formatter
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UIDatePicker
        datePicker.translatesAutoresizingMaskIntoConstraints = false
        datePicker.datePickerMode = .date  // 默认模式：日期
        datePicker.preferredDatePickerStyle = .wheels  // 滚轮样式（iOS 14+）
        datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
        view.addSubview(datePicker)
        
        // 创建日期显示标签
        dateLabel.translatesAutoresizingMaskIntoConstraints = false
        dateLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        dateLabel.textAlignment = .center
        dateLabel.text = "当前选择：\(dateFormatter.string(from: datePicker.date))"
        view.addSubview(dateLabel)
        
        // 创建模式选择分段控件
        modeSegmentedControl.translatesAutoresizingMaskIntoConstraints = false
        modeSegmentedControl.selectedSegmentIndex = 0  // 默认选中日期
        modeSegmentedControl.backgroundColor = .systemGray6
        modeSegmentedControl.selectedSegmentTintColor = .systemBlue
        modeSegmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        modeSegmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.label], for: .normal)
        modeSegmentedControl.addTarget(self, action: #selector(changeDatePickerMode(_:)), for: .valueChanged)
        view.addSubview(modeSegmentedControl)
        
        // 创建格式切换按钮
        formatButton.translatesAutoresizingMaskIntoConstraints = false
        formatButton.setTitle("切换格式", for: .normal)
        formatButton.backgroundColor = .systemBlue
        formatButton.tintColor = .white
        formatButton.layer.cornerRadius = 8
        formatButton.addTarget(self, action: #selector(toggleDateFormat), for: .touchUpInside)
        view.addSubview(formatButton)
    }
    
    // MARK: - 布局约束
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // DatePicker 约束
            datePicker.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            datePicker.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: margin * 3),
            
            // 日期标签约束
            dateLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin),
            dateLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin),
            dateLabel.topAnchor.constraint(equalTo: datePicker.bottomAnchor, constant: margin * 2),
            
            // 模式分段控件约束
            modeSegmentedControl.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin),
            modeSegmentedControl.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin),
            modeSegmentedControl.topAnchor.constraint(equalTo: dateLabel.bottomAnchor, constant: margin * 2),
            
            // 格式按钮约束
            formatButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            formatButton.topAnchor.constraint(equalTo: modeSegmentedControl.bottomAnchor, constant: margin * 2),
            formatButton.widthAnchor.constraint(equalToConstant: 150),
            formatButton.heightAnchor.constraint(equalToConstant: 44)
        ])
    }
    
    // MARK: - 交互逻辑
    @objc private func dateChanged(_ sender: UIDatePicker) {
        // 更新日期标签
        dateLabel.text = "当前选择：\(dateFormatter.string(from: sender.date))"
        
        // 可以在这里添加其他响应逻辑
        updateUIForDate(sender.date)
    }
    
    @objc private func changeDatePickerMode(_ sender: UISegmentedControl) {
        // 根据分段控件选择设置日期选择器模式
        switch sender.selectedSegmentIndex {
        case 0:
            datePicker.datePickerMode = .date
        case 1:
            datePicker.datePickerMode = .time
        case 2:
            datePicker.datePickerMode = .dateAndTime
        case 3:
            datePicker.datePickerMode = .countDownTimer
        default:
            break
        }
        
        // 更新标签显示
        dateChanged(datePicker)
    }
    
    @objc private func toggleDateFormat() {
        // 切换日期格式显示
        if dateFormatter.dateStyle == .medium {
            dateFormatter.dateStyle = .long
            dateFormatter.timeStyle = .medium
        } else {
            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .short
        }
        
        // 更新标签显示
        dateChanged(datePicker)
    }
    
    private func updateUIForDate(_ date: Date) {
        // 根据选择的日期改变背景色（示例效果）
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: date)
        
        if hour >= 6 && hour < 18 {
            // 白天
            view.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.9, alpha: 1.0)
        } else {
            // 夜晚
            view.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.2, alpha: 1.0)
            dateLabel.textColor = .white
        }
    }
}
